package com.manage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.manage.dto.OrderAndGoodsDto;
import com.manage.pojo.FlowerGoods;
import com.manage.pojo.FlowerOrder;
import com.manage.service.FlowerGoodsService;
import com.manage.pojo.FlowerStatistics;
import com.manage.mapper.FlowerStatisticsMapper;
import com.manage.service.FlowerOrderService;
import com.manage.service.FlowerStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class FlowerStatisticsServiceImpl extends ServiceImpl<FlowerStatisticsMapper, FlowerStatistics> implements FlowerStatisticsService {

    @Autowired
    private FlowerGoodsService flowerGoodsService;

    @Autowired
    private FlowerOrderService flowerOrderService;

    /**
     * 查询商户数据
     *
     * @param userId
     * @return
     */
    @Override
    @Transactional // 需要事务控制保持一致性
    public FlowerStatistics select_data(String userId) {
        FlowerStatistics statistics = null;
        boolean flag = true;
        while (true) {
            statistics = getOne(Wrappers.<FlowerStatistics>lambdaQuery().eq(FlowerStatistics::getUserId, userId));
            if (statistics == null){
                // 如果这里等于null就说明是刚注册的商户，我们需要给他生成对应的数据
                FlowerStatistics flowerStatistics = select_data_list(userId);
                save(flowerStatistics); // 保存数据
                flag = false;
            }else if(flag){
                // 每次登录需要重新修改一下数据
                FlowerStatistics flowerStatistics = select_data_list(userId);
                boolean b = updateById(flowerStatistics);
                if (b){
                    BeanUtils.copyProperties(flowerStatistics,statistics);
                    flag = false;
                }
            }else {
                break;
            }
        }
        return statistics;
    }

    /**
     * 查询商铺商品订单金额
     * @param userId
     * @return
     */
    public FlowerStatistics select_data_list(String userId) {
        // 1. 查询商铺的商品数量
        int goodsCount = flowerGoodsService.count(Wrappers.<FlowerGoods>lambdaQuery().eq(FlowerGoods::getUserId, userId));
        // 查询该商户的商品
        List<String> list = flowerGoodsService.select_user_count(userId);
        // 2. 查询订单数量
        int orderCount = flowerOrderService.count(Wrappers.<FlowerOrder>lambdaQuery().in(FlowerOrder::getGoodsId, list));
        // 计算销售额
        double price = flowerOrderService.select_price(list);
        // 计算存入库存的金额
        FlowerStatistics flowerStatistics = new FlowerStatistics(userId,BigDecimal.valueOf(price),BigDecimal.valueOf(price),goodsCount,orderCount);
        return flowerStatistics;
    }

    /**
     * 图表查询
     *
     * @param userId
     * @return
     */
    @Override
    public List<OrderAndGoodsDto> order_goods(String userId) {
        List<OrderAndGoodsDto> dtos = flowerGoodsService.order_goods(userId); // 调用商品service
        return dtos;
    }
}
